package org.scau.studentevaluation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.scau.studentevaluation.entity.bo.OtherActBO;
import org.scau.studentevaluation.entity.dos.OtherActDO;
import org.scau.studentevaluation.entity.query.AppealOtherActQuery;
import org.scau.studentevaluation.entity.vo.OtherActVO;
import org.scau.studentevaluation.service.IOtherActServicePart2;
import org.scau.studentevaluation.service.ImportMapperService;
import org.scau.studentevaluation.util.BeanConvertUtils;
import org.scau.studentevaluation.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class OtherActServicePart2Impl implements IOtherActServicePart2 {

    @Autowired
    private ImportMapperService importMapperService;

    @Override
    public List<OtherActVO> getUnCheckedOtherActivities(Long studentId) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getStudentId, studentId)
                .eq(OtherActDO::getOtherActYear, DateUtil.YEAR())
                .eq(OtherActDO::getIsChecked, 0);
        List<OtherActDO> DOs = importMapperService.otherActMapper.selectList(query);
        List<OtherActBO> BOs = BeanConvertUtils.DOs2BOs(DOs, OtherActBO.class);
        List<OtherActVO> VOs = BeanConvertUtils.BOs2VOs(BOs, OtherActVO.class);
        return VOs;
    }

    @Override
    public List<OtherActVO> getUnPassedOtherActivities(Long studentId) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getStudentId, studentId)
                .eq(OtherActDO::getOtherActYear, DateUtil.YEAR())
                .eq(OtherActDO::getIsChecked, 1)
                .eq(OtherActDO::getIsPassed, 0);
        List<OtherActDO> DOs = importMapperService.otherActMapper.selectList(query);
        List<OtherActBO> BOs = BeanConvertUtils.DOs2BOs(DOs, OtherActBO.class);
        List<OtherActVO> VOs = BeanConvertUtils.BOs2VOs(BOs, OtherActVO.class);
        return VOs;
    }

    @Override
    @Transactional
    public int deleteActivities(List<Integer> otherActivityIds) {
        int result = 1;
        for (Integer integer : otherActivityIds) {
            OtherActDO DO = importMapperService.otherActMapper.selectById(integer);
            if (DO == null) {
                result = 0;
                continue;
            }
            if (importMapperService.otherActMapper.deleteById(integer) == 0) {
                result = 0;
                continue;
            }
            String url = DO.getOtherActImageUrl();
            String[] temp = url.split("/");
            String filepath = "static/" + temp[temp.length - 1];
            File file = new File(filepath);
            if (file.isFile() && file.exists()) {
                file.delete();
            }
        }
        return result;
    }

    @Override
    @Transactional
    public int appealOtherActivity(AppealOtherActQuery appealOtherActQuery) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getId, appealOtherActQuery.getAppealActivityId())
                .eq(OtherActDO::getIsAppealed, 0)
                .eq(OtherActDO::getIsChecked, 1);
        OtherActDO DO = importMapperService.otherActMapper.selectOne(query);
        if (DO != null) {
            DO.setAppealRemark(appealOtherActQuery.getAppealRemark());
            DO.setIsAppealed(1);
            DO.setIsChecked(0);
        }
        return importMapperService.otherActMapper.updateById(DO);
    }

    @Override
    public Integer OtherActIsCheck(Long otherActId) {
        LambdaQueryWrapper<OtherActDO> query = Wrappers.<OtherActDO>lambdaQuery()
                .eq(OtherActDO::getId, otherActId)
                .eq(OtherActDO::getOtherActYear, DateUtil.YEAR());
        OtherActDO DO = importMapperService.otherActMapper.selectOne(query);
        if (DO != null) {
            return DO.getIsChecked();
        }
        return 0;
    }
}
